/*
 * LEDActuator.java
 *
 * Created on June 16, 2007, 8:49 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package atag.runtime.actuators;

import com.sun.spot.sensorboard.IDemoBoard;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import java.io.IOException;

/**
 *
 * @author Sirius
 */
public class LEDActuator implements ATaGActuator {
    
    private IDemoBoard sensorBoard;
    private ITriColorLED[] LEDs;
    //private int leds
    /** Creates a new instance of LEDActuator */
    
    private int maxValueLED;
    private int currentValueLED;
    private boolean isLEDEnable;
            
    public LEDActuator() {
        this.sensorBoard = EDemoBoard.getInstance();
        this.LEDs = sensorBoard.getLEDs();
        this.isLEDEnable = true;
        this.initializeLED();
    }
    
    private void initializeLED() {  
        for (int i = 0; i < 8; i++) {
            LEDs[i].setColor(LEDColor.BLUE);
            LEDs[i].setOff();			// turn off all LEDs
        }
        LEDs[0].setOn();
        LEDs[0].setColor(LEDColor.GREEN);
    }

    /**
     * for redrawing the LEDs
     * 
     */
    private void reDrawLEDs(){
        if (this.isLEDEnable == false) {
             for (int i = 1; i < 8; i++) {
                LEDs[i].setOff();			// turn off all display LEDs
             }
        }
        else {
             for (int i = 1; i < 8; i++) {
                    LEDs[i].setOn();   
                    LEDs[i].setColor(LEDColor.BLUE);
                    //Utils.sleep(200);
                    //leds[i].setOff();
                    //Utils.sleep(200);
             }
        }
    }
    
    public void disableLEDs() {
        this.isLEDEnable = false;
        this.reDrawLEDs();
    }
    
    public void enableLEDs() {
        this.isLEDEnable = true;
        this.reDrawLEDs();
    }

    public void reDrawSomeLEDs(double fraction) {

        this.isLEDEnable = true;
        if (fraction > 1.0) {
            fraction = 1;
        }

        int count = 1;
        int limit = (int) (7 * fraction) + 1; //[100 -> 8]
        while (count < limit) {
            LEDs[count].setOn();
            LEDs[count].setColor(LEDColor.BLUE);
            count++;
        }
        while (count < 8) {
            //switch off remaining LEDs
            LEDs[count].setOff();
            count++;
        }

    }
    
}
